package JDBC.Day09_Druid.Demo2;

import JDBC.Day09_Druid.utils.JDBCUtils;
import jdk.nashorn.internal.scripts.JD;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *  查询account表中的记录
 *  并封装进对象中
 *  装载进集合里
 */
public class SelectDemo {
    public static void main(String[] args) {

        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<User> list = new ArrayList<>();
        User user = null;

        try {
            //1. 获取连接
            conn = JDBCUtils.getConnection();
            //2. 定义预编译的sql语句
            pstmt = conn.prepareStatement("SELECT * FROM account");
            //3. 执行sql
            rs = pstmt.executeQuery();
            while(rs.next()) {
                user = new User();
                user.setId(rs.getInt("id"));
                user.setName(rs.getString("name"));
                user.setBalance(rs.getDouble("balance"));
                list.add(user);
            }
            System.out.println(list);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //4. 释放资源
            JDBCUtils.close(rs, pstmt, conn);
        }
    }
}
